﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SStuff.FlowControls
{
    [StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(FlowItem3D))]
    public class FlowItemsControl3D : FlowItemsControlBase
    {
        public FlowItemsControl3D()
        {
            this.DefaultStyleKey = typeof(FlowItemsControl3D);
        }

        FlowPanel3D _panel;


        protected override double GetNormalizedGestureOffset(Point mousePosition)
        {
            var intermediate = LayoutAlgorithm.GetNormalizedOffsetAtPoint(mousePosition);
            //var consideredItems = Math.Min(Items.Count, LayoutAlgorithm.GetMaxDisplayedItems());
            return intermediate;
        }

        protected override int GetConsideredItems()
        {
            return Math.Min(Items.Count, LayoutAlgorithm.GetMaxDisplayedItems());
        }

        protected override bool HandleGestures
        {
            get { return LayoutAlgorithm != null && LayoutAlgorithm.SupportsGestures; }
        }

        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
            if (_panel != null)
                _panel.ItemCount = Items.Count;
        }

        public Style ItemContainerStyle
        {
            get { return (Style)GetValue(ItemContainerStyleProperty); }
            set { SetValue(ItemContainerStyleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemContainerStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemContainerStyleProperty =
            DependencyProperty.Register("ItemContainerStyle", typeof(Style), typeof(FlowItemsControl3D), new PropertyMetadata(null));

        protected override DependencyObject GetContainerForItemOverride()
        {

            var container = new FlowItem3D();
            if (ItemContainerStyle != null)
                container.Style = ItemContainerStyle;
            return container;
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is FlowItem3D;
        }

       

        protected override void OnLayoutOffsetChanged()
        {
            base.OnLayoutOffsetChanged();
            if (_panel != null)
            {
                _panel.LayoutOffset = LayoutOffset;
            }
        }

        protected override void OnCyclicModeChanged()
        {
            base.OnCyclicModeChanged();
            if (_panel != null)
            {
                _panel.CyclicMode = CyclicMode;
            }
        }
        protected override void OnVirtualHostChanged()
        {
            base.OnVirtualHostChanged();
            _panel = VirtualHost as FlowPanel3D;
            if (_panel != null)
            {
                _panel.LayoutAlgorithm = this.LayoutAlgorithm;
                _panel.LayoutOffset = this.LayoutOffset;
                _panel.ItemCount = Items.Count;
                _panel.CyclicMode = CyclicMode;
            }
        }

        private void OnLayoutAlgorithmChanged()
        {
            if (_panel != null)
            {
                _panel.LayoutAlgorithm = LayoutAlgorithm;
            }
        }



        public IFlowLayout3DAlgorithm LayoutAlgorithm
        {
            get { return (IFlowLayout3DAlgorithm)GetValue(LayoutAlgorithmProperty); }
            set { SetValue(LayoutAlgorithmProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LayoutAlgorithm.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LayoutAlgorithmProperty =
            DependencyProperty.Register("LayoutAlgorithm", typeof(IFlowLayout3DAlgorithm), typeof(FlowItemsControl3D), new PropertyMetadata(new CoverFlow3D(),
                (s, a) =>
                {
                    (s as FlowItemsControl3D).OnLayoutAlgorithmChanged();
                }));

    }
}
